﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPTHelper
{
    partial class PPTHelper
    {

        #region //WebSocket服务
        public Dictionary<string, Func<string, string[], WsResutl>> ActionFunc { get; private set; }
        WsServer wsServer;
        private void WsSvr()
        {
            wsServer = new WsServer(52528);
            wsServer.RouteFunc.Add("/PPTHelper", (msg) => WsMain(msg));
            wsServer.Start();

            ActionFunc = new Dictionary<string, Func<string, string[], WsResutl>>
            {
                { "Data", (cmd,args) => new WsResutl(GetData().ToJson()) },
                { "Play", (cmd,args) => { Play(); return null; } },
                { "Stop", (cmd,args) => { Stop(); return null; } },
                { "First", (cmd,args) => { First(); return null; } },
                { "Last", (cmd,args) => { Last(); return null; } },
                { "Previous", (cmd,args) => { Previous(); return null; } },
                { "Next", (cmd,args) => { Next(); return null; } },
                { "Screen0", (cmd,args) => WsGetScreen(0) },
                { "Screen1", (cmd,args) => WsGetScreen(1) },
                { "LeftClick", (cmd,args) => { Win32Api.MouseLeftClick(); return null; } },
                { "RightClick", (cmd,args) => { Win32Api.MouseRightClick(); return null; } },
                { "MouseMove", WsSetMouse },
                { "KeyPress", WsKeyPress },
                { "KeyDown", WsKeyDown },
                { "KeyUp", WsKeyUp },
                { "SendKey", WsSendKeys },
            };
        }


        //广播
        public void WsBroadcast(string msg)
        {
            Log.Info("WebSocketBroadcast：string");
            wsServer.Broadcast(msg);
        }

        public void WsBroadcast(byte[] bytes)
        {
            Log.Info("WebSocketBroadcast：bytes");
            wsServer.Broadcast(bytes);
        }

        WsResutl WsMain(string msg)
        {
            //返回值
            WsResutl result = null;
            if (string.IsNullOrEmpty(msg)) return result;

            //分割命令，解析参数
            var list = msg.Split(',').ToList();
            var command = list[0];
            list.RemoveAt(0);
            var args = list.ToArray();

            //回调
            if (ActionFunc.ContainsKey(command))
            {
                result = ActionFunc[command]?.Invoke(command, args);
            }
            return result;
        }

        //鼠标动作
        [Description(",0,100,100,1920,1080#屏幕号,坐标x,坐标y,屏宽w,屏高h")]
        public WsResutl WsSetMouse(string command, string[] args)
        {
            //解析参数
            var list = new List<int>();
            Array.ForEach(args, m => { float.TryParse(m, out float n); list.Add((int)n); });

            if (list.Count == 5)
            {
                var pt = PrintScreen.GetMouseAbsolute(list[0], list[1], list[2], list[3], list[4]);
                switch (command)
                {
                    case "MouseMove":
                        Win32Api.MouseMove(pt);
                        break;
                    case "LeftClick":
                        Win32Api.MouseLeftClick(pt);
                        break;
                    case "RightClick":
                        Win32Api.MouseRightClick(pt);
                        break;
                    default:
                        break;
                }
            }
            return null;
        }


        //屏幕
        [Description(",0#屏幕号0-1")]
        public WsResutl WsGetScreen(int i)
        {
            var bytes = PrintScreen.GetScreenImage(i);
            var result = new WsResutl(bytes);
            result.Headers.Add("content-type", "image/jpeg");
            result.Headers.Add("Cache-Control", "no-cache");
            return result;
        }


        //按键
        [Description(",91#左Windows键 <a href='https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes'>虚拟键代码<a>")]
        public WsResutl WsKeyPress(string command, string[] args)
        {
            //解析参数
            if (args.Length == 1 && byte.TryParse(args[0], out byte vk))
            {
                switch (command)
                {
                    case "KeyDown":
                        Win32Api.KeyDown(vk);
                        break;
                    case "KeyUp":
                        Win32Api.KeyUp(vk);
                        break;
                    default:
                        Win32Api.KeyPress(vk);
                        break;
                }
            }
            return null;
        }

        [Description(",0#按下")]
        public WsResutl WsKeyDown(string command, string[] args)
        {
            return WsKeyPress("KeyDown", args);
        }

        [Description(",0#弹起")]
        public WsResutl WsKeyUp(string command, string[] args)
        {
            return WsKeyPress("KeyUp", args);
        }

        //发送的击键字符串
        [Description(",{F12}#F12键 <a href='https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.forms.sendkeys.send?view=netframework-4.6'>发送按键字符串<a>")]
        public WsResutl WsSendKeys(string command, string[] args)
        {
            //解析参数
            if (args.Length == 1)
            {
                var keys = args[0];
                Task.Run(() => System.Windows.Forms.SendKeys.SendWait(keys));
            }
            return null;
        }

        #endregion




    }
}
